package beans;

import java.util.*;

public class ShoppingBean {
    private Collection cart;
    
    /** Creates a new instance of ShoppingBean */

    public ShoppingBean() {
        cart = new ArrayList();
    }
    
    /** According to chosen details add computers to the shopping cart
     * 
     * @param hddBean
     * @param motherboardBean
     * @param processorBean
     * @param ramBean
     * @param videocardBean 
     */

    public void addComputer(HDDBean hddBean,
                            MotherboardBean motherboardBean,
                            ProcessorBean processorBean,
                            RAMBean ramBean,
                            VideocardBean videocardBean) {
        
        Object newItem[] = null;
        
	// if the cart is empty just add the computer

        if (cart.isEmpty()){
            newItem = new Object[7];
            newItem[0]=hddBean;
            newItem[1]=motherboardBean;
            newItem[2]=processorBean;
            newItem[3]=ramBean;
            newItem[4]=videocardBean;
            newItem[5]=1;
            newItem[6]=hddBean.getPrice()+
                       motherboardBean.getPrice()+
                       processorBean.getPrice()+
                       ramBean.getPrice()+
                       videocardBean.getPrice();
            cart.add(newItem);
        }
 
	// otherwise we need to check if this computer already
	// is in the cart

        else{
	    Iterator iter = cart.iterator();  // get an iterator
	    Object tmpArr[];
	    boolean found = false;
	    while(iter.hasNext()){
		tmpArr=(Object[])iter.next();

		// check if we found the computer

		if(((HDDBean)tmpArr[0]).getId()== hddBean.getId()&&
                   ((MotherboardBean)tmpArr[1]).getId()== motherboardBean.getId()&&
                   ((ProcessorBean)tmpArr[2]).getId()== processorBean.getId()&&
                   ((RAMBean)tmpArr[3]).getId()== ramBean.getId()&&
                   ((VideocardBean)tmpArr[4]).getId()== videocardBean.getId()){ 
                                  
		    // yes, increase the quantity

		    Integer tmpAntal = (Integer)tmpArr[5];
		    tmpArr[5] = new Integer(tmpAntal.intValue())+1; 
		    found = true;
                    
                    Integer tmpPrice= (Integer)tmpArr[6];
                    tmpArr[6]= new Integer(tmpPrice + hddBean.getPrice()+
                       motherboardBean.getPrice()+
                       processorBean.getPrice()+
                       ramBean.getPrice()+
                       videocardBean.getPrice());
		}
		
	    }
	    
	    // if we didn't find it, add it
	    
	    if(!found){
                newItem = new Object[7];
                newItem[0]=hddBean;
                newItem[1]=motherboardBean;
                newItem[2]=processorBean;
                newItem[3]=ramBean;
                newItem[4]=videocardBean;
                newItem[5]=1;
                newItem[6]=hddBean.getPrice()+
                       motherboardBean.getPrice()+
                       processorBean.getPrice()+
                       ramBean.getPrice()+
                       videocardBean.getPrice();
		cart.add(newItem);
	    }
	    
        }          
    }    

    /** Remove some copies of a computer from the cart
     * 
     * @param hdd_id - id of hdd
     * @param mb_id - id of motherboard
     * @param proc_id - id of processor
     * @param ram_id - id of ram
     * @param vc_id - id of videocard
     */

    public void removeComputer(int hdd_id, 
                               int mb_id, 
                               int proc_id, 
                               int ram_id,
                               int vc_id) {

	// if must not be empty

        if(!cart.isEmpty()){
            Iterator iter = cart.iterator();
            Object tmpArr[];

	    // search for the computer

            while(iter.hasNext()){
                tmpArr=(Object[])iter.next();
                
                if(((HDDBean)tmpArr[0]).getId()== hdd_id&&
                   ((MotherboardBean)tmpArr[1]).getId()== mb_id&&
                   ((ProcessorBean)tmpArr[2]).getId()== proc_id&&
                   ((RAMBean)tmpArr[3]).getId()== ram_id&&
                   ((VideocardBean)tmpArr[4]).getId()== vc_id){ 
                    
          	    // found

                    Integer tmpAntal = (Integer)tmpArr[5];
                    Integer tmpPrice = (Integer)tmpArr[6];

		    // if all copies removed, remove the computer
		    // from the cart

                    if(tmpAntal.intValue()==1){
                        iter.remove();
                        tmpArr[6]=0;
                    }
                    else{

	            // else reduce quantity
                    tmpArr[5]=new Integer(tmpAntal.intValue() - 1);
                    // reduce price
                    tmpArr[6]= new Integer(tmpPrice - ((HDDBean)tmpArr[0]).getPrice()-
                       ((MotherboardBean)tmpArr[1]).getPrice()-
                       ((ProcessorBean)tmpArr[2]).getPrice()-
                       ((RAMBean)tmpArr[3]).getPrice()-
                       ((VideocardBean)tmpArr[4]).getPrice());
                    
                    }
                }
            }
        }
    }     
    
    // clear the cart

    void clear() {
        cart.clear();
    }
    
    /**
     * Create an XML document out of the shopping cart
     * @return - order XML document
     */

    public String getXml(){
        StringBuffer buff = new StringBuffer();
        
        Iterator iter = cart.iterator();
        Object objBuff[] = null;
        buff.append("<shoppingcart>");
        while(iter.hasNext()){
            objBuff =(Object[])iter.next();
            buff.append("<order>");
            buff.append(((HDDBean)objBuff[0]).getXml());
            buff.append(((MotherboardBean)objBuff[1]).getXml());
            buff.append(((ProcessorBean)objBuff[2]).getXml());
            buff.append(((RAMBean)objBuff[3]).getXml());
            buff.append(((VideocardBean)objBuff[4]).getXml());
            buff.append("<quantity>");
            buff.append(((Integer)objBuff[5]).intValue());
            buff.append("</quantity>");
            buff.append("<price>");
            buff.append(((Integer)objBuff[6]).intValue());
            buff.append("</price>");
            buff.append("</order>");            
        }
        buff.append("</shoppingcart>");
        return buff.toString();
    }

    // get the cart

    public Collection getCart(){
      return cart;
    }
    
}
